from src.exts import db
from src.models.user_model import User


def create_user(data):
    try:
        user = User(
            username = data.get("username"),
            password = data.get("password")
        )
        db.session.add(user)
        db.session.commit()
        return user
    except Exception as e:
        db.session.rollback()
        print(str(e))
        return str(e)

def search_user(data):
    username = data.get("username")
    try:
        user = User.query.filter_by(username=username).first()
        return user
    except Exception as e:
        print("[ 数据库错误 ] error: {}".format(str(e)))
        return str(e)

def get_user(user_id):
    try:
        user = User.query.get(user_id)
        return user
    except Exception as e:
        print(str(e))
        return str(e)